import java.util.HashSet;
import java.util.Set;

public class Solution {
    public static boolean isHappy(int n) {
        //2^31 - 1   2147483647
        Set<Integer> set = new HashSet<>();
        while (n != 1 && !set.contains(n)) {
            set.add(n);
            n = getHappy(n);
        }
        return n == 1;
    }
    public static int getHappy(int n) {
        int  ret = 0;
        while (n > 0) {
            int i = n % 10;
            ret += i * i;
            n = n / 10;
        }
        return ret;
    }

    public static void main(String[] args) {
        System.out.println(isHappy(2));
    }
}
